package com.happywynne.elasticsearchtravel.controller;

import com.happywynne.elasticsearchtravel.pojo.TrashBinTravel;
import com.happywynne.elasticsearchtravel.service.FakerService;
import com.happywynne.elasticsearchtravel.util.ElasticSearchUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author Wynne.Lu
 * @date 2021/1/24 下午5:48
 * @desc
 */
@RestController
@Slf4j
@RequestMapping("/fake")
public class FakerController {

    @Autowired
    private FakerService fakerService;

    private static final String INDEX_NAME = "bin-travel";

    @PostMapping("/insert")
    public void insertFakeTrashBinTravel() {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int k = 0; k < 5_000_000; k++) {
                    log.info(String.valueOf(k));
                    TrashBinTravel binTravel = fakerService.genFakeData();
                    try {
                        ElasticSearchUtil.insert(INDEX_NAME, binTravel);
                    } catch (IOException e) {
                        log.error(e.getMessage());
                    }
                }
            }).start();
        }
    }

    @PostMapping("/create-index")
    public void createBinTravelIndex() throws IOException {
        if (!ElasticSearchUtil.existsIndex(INDEX_NAME)) {
            ElasticSearchUtil.createIndex(INDEX_NAME);
        }
    }
}
